//
//  TXFEmoticonCollectionViewCell.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/7/5.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit

class TXFEmoticonCollectionViewCell: UICollectionViewCell {
    
    
    var emoticons: [TXFEmoticon]?{
        didSet{
        //第一开始时候先隐藏所有按钮避免重用:
           
            
            for  button in emoticonButtons{
            
            button.hidden = true
            
            }
            
        
        //遍历外界传入的一维数组:
            
            for (i,emoticon) in emoticons!.enumerate(){
            //拿到数组中的按钮
               let  button = emoticonButtons[i]
                
               button.hidden = false
              button.emoticon = emoticon
                if emoticon.isEmoji{
                
               let tit = ((emoticon.code ?? "") as NSString).emoji()
                
                    button.setTitle(tit, forState: UIControlState.Normal)
                    
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                
                }else{
                
                let path = emoticon.path ?? ""
                    
                    
                let png = emoticon.png ?? ""
                   
                let name = path + png
                    
                let image = UIImage(named: name, inBundle: TXFEmoticonTools.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
                
                button.setImage(image, forState: UIControlState.Normal)
                    
                button.setTitle(nil, forState: UIControlState.Normal)
                
                
                
                }

            
            }

        
        }

    }
    
    
    //点击事件
    
    @objc private func buttonClick(button: TXFEmoticonButton){
    
    //添加通知:
    
    NSNotificationCenter.defaultCenter().postNotificationName(EmoticonButtonNoti, object: button.emoticon)
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    //按钮的一个可变数组(可以进行修改)
    
    var emoticonButtons: [TXFEmoticonButton] = [TXFEmoticonButton]()
    
    
    func  setupUI(){
    
    self.backgroundColor = UIColor.whiteColor()
    
       
    contentView.addSubview(deleteButton)
    addChildButtons()
    }
    
    //设置frame:
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        
        let  w = (SCREENW - 10) / CGFloat(EmoticonMaxCol)
        
        let h = (179 - 20) / CGFloat(EmoticonMaxRow)
        
        for (i,button) in emoticonButtons.enumerate(){
        
        let x = i % EmoticonMaxCol
        
        let y = i / EmoticonMaxCol
        
            button.frame = CGRect(x: CGFloat(x)*w+5, y: CGFloat(y)*h, width: w, height: h)
            
        }
        
        deleteButton.frame = CGRect(x: SCREENW - w - 5, y: h*2, width: w, height: h)
        
        
        
        
    }
    
    
    
    
    //创建20个按钮
    
    func addChildButtons(){
    
        for _ in 0..<EmoticonMaxCount{
        let  button = TXFEmoticonButton()
            
            //添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
        button.titleLabel?.font = UIFont.systemFontOfSize(32)
//          button.backgroundColor = RandomColor()
        //
            
            
        emoticonButtons.append(button)
            
            
        contentView.addSubview(button)

        }

    }
    //删除按钮点击事件
    
    func  deleteButtonClick(){

    
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonDeleteNoti, object: nil)

    
    }
    
    
    //懒加载删除按钮:
    
    private lazy var deleteButton: UIButton = {
    let  button = UIButton()
    button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
    button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
    
        
    button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    
    return button
    }()
    
    
    
    
}
